using QFramework;
using System;
using UnityEngine;

public abstract class AGVObject : MonoBehaviour, ISelectable
{
    public BindableProperty<NdcInfo> AgvData = new();

    public string Agvid;
    public float Time_To_Move, Time_To_Lift;

    public AgvType AgvType;

    [Range(0, 360)]
    public float directionModify = 270;

    public Map mapInScene;

    [SerializeField]
    protected Transform LiftPart;

    [SerializeField]
    public Transform zeroPoint;

    [SerializeField]
    protected Transform touchCargoColliderBox;

    protected AgvMoveComponent moveComponent;
    protected AgvLiftComponent liftComponent;
    protected AgvOtherBehaviour OtherBehaviourComponent;
    protected AgvCargoTriggerComponent TouchCargoComponent;

    //protected GetSingleCarDetailDataBehaviour GetSingleCarDetailDataBehaviour;

    protected float AxisScaleFactor = 1000;

    protected bool loaded = false;

    public float LiftingSpeed { get; private set; }
    [HideInInspector]
    public float MovingSpeed { get; private set; }

    public bool LayerVisible { get; set; }

    public abstract Action OnSelected { get; set; }
    public abstract Action OnDeselected { get; set; }
    public abstract Collider Collider { get; set; }

    protected virtual void OnEnable()
    {
        AgvData.WithComparer(AgvDataComparer);
        AgvData.Register(OnAgvDataChanged);
    }
    private bool AgvDataComparer(NdcInfo a, NdcInfo b)
    {
        //Debug.Log($"a == b = {b != null && a.agvName == b.agvName && a.agvid == b.agvid  && a.agvTask_status == b.agvTask_status && a.angle == b.angle && a.battery == b.battery && a.blocked == b.blocked&& a.free == b.free&& a.height == b.height                && a.levelValue == b.levelValue                && a.loaded == b.loaded                && a.move == b.move                && a.online == b.online                && a.osz == b.osz                && a.posX == b.posX                && a.posY == b.posY                && a.reliable == b.reliable                && a.revise == b.revise                && a.sensor == b.sensor                && a.status == b.status                && a.taskStatus == b.taskStatus                && a.type == b.type                && a.wheelangle == b.wheelangle                && a.wheelleft == b.wheelleft                && a.wheelright == b.wheelright}");
        return b != null
            && a.agvId == b.agvId
            && a.posX == b.posX
            && a.posY == b.posY
            && a.angle == b.angle
            && a.pushStatus == b.pushStatus
            && a.forkHeight == b.forkHeight
            && a.isLoaded == b.isLoaded
            && a.isStop == b.isStop
            && a.isBlock == b.isBlock;
    }
    private void Start()
    {
        //moveComponent = new(transform);
        //liftComponent = new(LiftPart);
        //OtherBehaviourComponent = new();
    }

    protected virtual void Update()
    {
        moveComponent?.Update();
        liftComponent?.Update();
        TouchCargoComponent?.Update(loaded);
        OtherBehaviourComponent?.Update();

        LiftingSpeed = liftComponent.GetLiftingSpeed();
    }

    private void OnAgvDataChanged(NdcInfo vehicleData)
    {
        //Debug.Log($"Trigger");
        CaculatingVehicleTargetTransformData(vehicleData);
        //GetAllocatedResources(vehicleData);
    }
    /*private void OnAgvDataChanged(IVehicleData vehicleData)
    {
        //Debug.Log($"Trigger");
        CaculatingVehicleTargetTransformData(vehicleData);
        GetAllocatedResources(vehicleData);
    }*/


    protected virtual void OnDestroy()
    {

    }
    /*private void OnDestroy()
    {
        UnRegister();
        //WebSocketSingleDataProcessedEvent.UnRegister(GetCarDetailDataByWebSocket);
        //HTTPRequestProcessedEvent.UnRegister(GetCarDetailDataByCarHTTP);
    }*/
    /*protected virtual void UnRegister()
    {
        GetSingleCarDetailDataBehaviour.UnRegisterGetCarDetailDataEvent(GetCarDetailDataByWebSocket);
    }*/
    /*private void GetCarDetailDataByWebSocket(DigitalTwin.DigitalModel.CarData dataFromTcs)
    {
        if (dataFromTcs != null && Config.CONNECTION_TYPE == connectionType.ws && dataFromTcs.agvid.ToString() == Agvid)
        {
            CaculatingVehicleTargetTransformData(dataFromTcs);
        }
        //throw new NotImplementedException();
    }*/

    public virtual void CaculatingVehicleTargetTransformData(NdcInfo info)
    {
        Vector3 targetPosition = zeroPoint.rotation * new Vector3(info.posX / AxisScaleFactor, transform.position.y, info.posY / AxisScaleFactor) + new Vector3(zeroPoint.position.x, 0, zeroPoint.position.z);
        float targetDirection = directionModify - info.angle;
        moveComponent.SetTargetTransform(targetPosition: targetPosition, direction: targetDirection);
        liftComponent.SetTargetHeight(info.forkHeight);
    }

    bool PreviousLoaded = false;

    bool IsInit = false;
    public void CheckCargoLoaded(bool isLoaded)
    {
        if (!IsInit)
        {
            IsInit = true;
            if (isLoaded == true)
            {
                TouchCargoComponent.PickCargo();
            }
            else
            {
                TouchCargoComponent.PutCargo();
            }
            TouchCargoComponent.SetLoadedValueWithoutEvent(isLoaded);
            PreviousLoaded = loaded = isLoaded;
        }
        else if (isLoaded != PreviousLoaded)
        {
            PreviousLoaded = loaded = isLoaded;
        }
    }
    public AgvCargoTriggerComponent GetTouchCargo()
    {
        return TouchCargoComponent;
    }


}